//
//  SunLoginCheckVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/17.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunLoginCheckVC.h"
#import "SunForgetCodeVC.h"
#undef Y
#define Y(y) NY(y)
@interface SunLoginCheckVC ()<UITextFieldDelegate>
{
    UITextField *_code;
    UIButton *_securityBtn;
    UIButton *_loginBtn;
}
@end

@implementation SunLoginCheckVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)creatUI{
    UIView *bgView = [[UIView alloc]init];
    [self.view addSubview:bgView];
    [bgView lt_Top:SunEdgesMake(0, 0, 0, Y(210)) to:nil save:YES];
    bgView.backgroundColor = BLUE;
    UIImageView *head = [[UIImageView alloc]init];
    [bgView addSubview:head];
    [head lt_layoutView:head side:SunCenterX to:nil toSide:SunCenterX withSize:1 offset:Y(-2) save:YES];
    [head lt_layoutView:head side:SunCenterY to:nil toSide:SunCenterY withSize:1 offset:Y(10) save:YES];
    [head lt_setSize:CGSizeMake(X(115), X(115)) save:YES];
    head.image = [UIImage imageNamed:@"login_head"];
    UIView *bar = [[UIView alloc]init];
    [bgView addSubview:bar];
    [bar lt_Top:SunEdgesMake(20, 0, 0, 44) to:nil save:YES];
    UIButton *returnBtn = [[UIButton alloc]init];
    [bar addSubview:returnBtn];
    [returnBtn lt_YcenterTo:nil save:YES];
    [returnBtn lt_setLeft:X(6) to:nil save:YES];
    [returnBtn setImage:[UIImage imageNamed:@"tool_back"] forState:UIControlStateNormal];
    [returnBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UILabel *title = [[UILabel alloc]init];
    [bar addSubview:title];
    [title lt_XYcenterTo:nil save:YES];
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = WHITE;
    title.font = [UIFont boldSystemFontOfSize:19];
    title.text = @"登录";
    UILabel *phone = [[UILabel alloc]init];
    [bgView addSubview:phone];
    [phone lt_XcenterTo:nil save:YES];
    [phone lt_setTop:Y(6) to:head save:YES];
    phone.textColor = WHITE;
    phone.textAlignment = NSTextAlignmentCenter;
    phone.font = [UIFont systemFontOfSize:16];
    phone.text = [[[self.phone substringToIndex:3]stringByAppendingString:@"****"]stringByAppendingString:[self.phone substringFromIndex:7]];
    UIView *whiteBg = [[UIView alloc]init];
    [self.view addSubview:whiteBg];
    [whiteBg lt_Top:SunEdgesMake(Y(20), 0, 0, Y(72)) to:bgView save:YES];
    whiteBg.backgroundColor = WHITE;
    UIImageView *codeImg = [[UIImageView alloc]init];
    [whiteBg addSubview:codeImg];
    codeImg.image = [UIImage imageNamed:@"login_code"];
    [codeImg lt_YcenterTo:nil save:YES];
    [codeImg lt_setLeft:X(18) to:nil save:YES];
    _code = [[UITextField alloc]init];
    _code.delegate = self;
    [whiteBg addSubview:_code];
    [_code lt_YcenterTo:nil save:YES];
    [_code lt_setLeft:X(10) to:codeImg save:YES];
    [_code lt_setWidth:X(275) save:YES];
    _code.placeholder = @"请输入您的登录密码";
    _code.textColor = BLACK;
    _code.font = [UIFont systemFontOfSize:18];
    _code.clearButtonMode = UITextFieldViewModeWhileEditing;
    _code.returnKeyType=UIReturnKeyDone;
    _code.secureTextEntry = YES;
    _securityBtn = [[UIButton alloc]init];
    [whiteBg addSubview:_securityBtn];
    [_securityBtn lt_YcenterTo:nil save:YES];
    [_securityBtn lt_setRight:X(15) to:nil save:YES];
    [_securityBtn setImage:[UIImage imageNamed:@"login_security"] forState:UIControlStateNormal];
    [_securityBtn setImage:[UIImage imageNamed:@"login_unsecurity"] forState:UIControlStateSelected];
    [_securityBtn addTarget:self action:@selector(changeSecurity) forControlEvents:UIControlEventTouchUpInside];
    _loginBtn = [[UIButton alloc]init];
    [self.view addSubview:_loginBtn];
    [_loginBtn lt_Top:SunEdgesMake(Y(56), X(15), X(15), Y(44)) to:whiteBg save:YES];
    _loginBtn.clipsToBounds = YES;
    [_loginBtn setBackgroundColor:BLUE forState:UIControlStateNormal];
    [_loginBtn setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
    _loginBtn.layer.cornerRadius = 4;
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn setTitleColor:WHITE forState:UIControlStateNormal];
    _loginBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [_loginBtn addTarget:self action:@selector(gotoLogin) forControlEvents:UIControlEventTouchUpInside];
    _loginBtn.enabled = NO;
    UIButton *forget = [[UIButton alloc]init];
    [self.view addSubview:forget];
    [forget lt_setRight:X(15) to:nil save:YES];
    [forget lt_setTop:Y(6) to:_loginBtn save:YES];
    [forget setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [forget setTitleColor:RED forState:UIControlStateNormal];
    [forget setTitleColor:[HXColor colorWithHexString:@"FFB6C1"] forState:UIControlStateHighlighted];
    [forget addTarget:self action:@selector(forgetCode) forControlEvents:UIControlEventTouchUpInside];
    forget.titleLabel.font = [UIFont systemFontOfSize:13];
}

- (void)forgetCode{
    SunForgetCodeVC *vc = [[SunForgetCodeVC alloc]init];
    vc.phone = self.phone;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoLogin{
    [[Sun shareSun]showActivity:self];
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

    NSString *urlStr = [NSString stringWithFormat:@"%@%@",KURL,API_LOGIN];
    NSDictionary *param = @{@"phone":self.phone,@"password":_code.text};
    [HttpRequest postURL:urlStr withParam:param success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        [Sun setUser:[requestDic objectForKey:@"accessKey"] forKey:MD5];
        [Sun setUser:[requestDic objectForKey:@"desKey"] forKey:DES];
        [Sun setUser:[requestDic objectForKey:@"accessId"] forKey:ACCESSID];
        [Sun setUser:@"1" forKey:KLOGIN];
        [Sun setUser:self.phone forKey:PHONE];
        [[Sun shareSun]showMsg:@"登录成功"];
        //跳转回登录前的界面
        if (self.isWeb) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {

            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] animated:YES];

        }

    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)changeSecurity{
    if (_securityBtn.isSelected) {
        _securityBtn.selected = NO;
        _code.secureTextEntry = YES;
    }else{
        _securityBtn.selected = YES;
        _code.secureTextEntry = NO;
    }
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *result = textField.text;
    if (string.length > 0 && textField.text.length < 6) {
        result = [textField.text stringByAppendingString:string];
    }else if (string.length <= 0 && result.length > 0){
        result = [textField.text substringToIndex:textField.text.length - 1];
    }
    if (result.length >= 6) {
        _loginBtn.enabled = YES;
    }else{
        _loginBtn.enabled = NO;
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    _loginBtn.enabled = NO;
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
